<?php

namespace frontend\models;

/**
 * @author  shuang
 * @date    2016-10-29 9:36:51
 * @version V1.0
 * @desc    
 */
use common\models\UserPayRecord;
use Yii;
use common\components\MTools;
use yii\helpers\ArrayHelper;
use yii\behaviors\TimestampBehavior;
use frontend\models\WB_UserAccount;

class WB_UserPayRecord extends UserPayRecord {

    //交易类型
    public static $paytype = [1 => "充值", 2 => "支出"];
    //交易类别
    public static $class_type = [
        1 => ["classnote" => "支付宝充值", "desc" => "皓夜任务平台充值"],
        2 => ["classnote" => "投诉返现", "desc" => "投诉返现"],
        3 => ["classnote" => "停止任务", "desc" => "停止任务返现"],
        4 => ["classnote" => "发布任务", "desc" => "测试"],
        5 => ["classnote" => "邀请返利", "desc" => "测试"],
        6 => ["classnote" => "追加任务", "desc" => "追加任务数量"],
        7 => ["classnote" => "其他", "desc" => "任务置顶或刷新"],
        8 => ["classnote" => "返利充值", "desc" => "将返利金额充值进账户"],
        9 => ["classnote" => "申诉退款", "desc" => "刷手任务申诉成功"]
    ];
    public $merchants_type = [
        1 => "充值", 2 => "付款", 3 => "退款"
    ];

    /*
     * 设置表操作行为动作
     * return array
     */

    public function behaviors() {
        return[
            [
                'class' => TimestampBehavior::className()
            ]
        ];
    }

    /*
     * 添加交易记录 、 更新账户金额
     * @params $userid
     * @params $amount
     * @params $type
     * @params $class_type
     * @params $note
     */

    public static function createRecord($userid, $amount, $type = 1, $class_type = 1, $note = null) {
        try {
            $userpayrecord = new WB_UserPayRecord();
            $userpayrecord->userid = $userid;
            $userpayrecord->type = $type;
            $userpayrecord->class_type = $class_type;
            $userpayrecord->amount = $amount;
            $userpayrecord->description = $note ? $note : ArrayHelper::getValue(WB_UserPayRecord::$class_type, "$class_type.desc");
            if ($userpayrecord->save()) {
                $uesraccount = WB_UserAccount::find()->where("userid=" . $userid)->one();
                if ($uesraccount) {
                    if ($class_type === 5) {
                        $uesraccount->rebate_amount = $uesraccount->rebate_amount + $amount;
                    }
                    $uesraccount->amount = $uesraccount->amount + $amount;
                    return $uesraccount->save();
                } else {
                    $useraccount = new WB_UserAccount();
                    $useraccount->userid = $userid;
                    $useraccount->amount = 0;
                    $useraccount->rebate_amount = 0;
                    return $useraccount->save();
                }
            } else {
                return false;
            }
        } catch (Exception $ex) {
            throw $ex;
        }
    }

    /*
     * 任务列表
     * @params $status
     * @params $title
     * return array
     */

    public static function getList() {
        $query = WB_UserPayRecord::find();
        $query->andWhere("userid=:userid", [":userid" => Yii::$app->user->id]);
        $statusType = Yii::$app->request->get("statusType");
        $begin_at = Yii::$app->request->get("begin_at");
        $end_at = Yii::$app->request->get("end_at");
        if ($statusType > 0) {
            if ($statusType == 1) {
                $class_type = [1, 5, 8];
            }
            if ($statusType == 2) {
                $class_type = [4, 6, 7, 9];
            }
            if ($statusType == 3) {
                $class_type = [2, 3];
            }
            $query->andFilterWhere(["in", "class_type", $class_type]);
        }
        if ($begin_at) {
            $query->andFilterWhere([">=", "created_at", strtotime($begin_at)]);
        }
        if ($end_at) {
            $query->andFilterWhere(["<=", "created_at", strtotime($end_at)]);
        }
        $query->orderBy("created_at desc");
        $countQuery = clone $query;
        $defaultPageSize = 20;
        $pager = new \yii\data\Pagination([
            'totalCount' => $countQuery->count(),
            'defaultPageSize' => $defaultPageSize,
            'params'=>[
                "statusType" => $statusType,
                "begin_at" => $begin_at,
                "end_at" => $end_at,
                "page"=>Yii::$app->request->get("page")
            ]
        ]);
        $offset = (Yii::$app->request->get("page") - 1) * $defaultPageSize;
        $limit = $defaultPageSize;
        $res = $query->offset($offset)->limit($limit)->asArray()->all();
        return ["pager" => $pager, "data" => $res, "statusType" => $statusType, "begin_at" => $begin_at, "end_at" => $end_at];
    }

    /*
     * 返利更新账号
     */

    public static function addrebateRecord() {
        
    }

}
